home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / antlr / hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  5.1 KB  |  215 lines  |  [TEXT/MPS ]

  1. /*
  2.  * hash.c
  3.  *
  4.  * $Id: hash.c,v 1.4 1994/08/29 20:16:14 parrt Exp parrt $
  5.  * $Revision: 1.4 $
  6.  *
  7.  * Manage hash tables.
  8.  *
  9.  * The following functions are visible:
  10.  *
  11.  *        char    *mystrdup(char *);        Make space and copy string
  12.  *        Entry     **newHashTable();        Create and return initialized hash table
  13.  *        Entry    *hash_add(Entry **, char *, Entry *)
  14.  *        Entry    *hash_get(Entry **, char *)
  15.  *
  16.  * SOFTWARE RIGHTS
  17.  *
  18.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  19.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  20.  * company may do whatever they wish with source code distributed with
  21.  * PCCTS or the code generated by PCCTS, including the incorporation of
  22.  * PCCTS, or its output, into commerical software.
  23.  * 
  24.  * We encourage users to develop software with PCCTS.  However, we do ask
  25.  * that credit is given to us for developing PCCTS.  By "credit",
  26.  * we mean that if you incorporate our source code into one of your
  27.  * programs (commercial product, research project, or otherwise) that you
  28.  * acknowledge this fact somewhere in the documentation, research report,
  29.  * etc...  If you like PCCTS and have developed a nice tool with the
  30.  * output, please mention that you developed it using PCCTS.  In
  31.  * addition, we ask that this header remain intact in our source code.
  32.  * As long as these guidelines are kept, we expect to continue enhancing
  33.  * this system and expect to make other tools available as they are
  34.  * completed.
  35.  *
  36.  * ANTLR 1.23
  37.  * Terence Parr
  38.  * Parr Research Corporation
  39.  * with Purdue University and AHPCRC, University of Minnesota
  40.  * 1989-1994
  41.  */
  42.  
  43. #include <stdio.h>
  44. #ifdef __cplusplus
  45. #ifndef __STDC__
  46. #define __STDC__
  47. #endif
  48. #endif
  49. #include "hash.h"
  50. #ifdef __STDC__
  51. #include <stdlib.h>
  52. #else
  53. #ifdef VAXC
  54. #include <stdlib.h>
  55. #else
  56. #include <malloc.h>
  57. #endif
  58. #endif
  59. #include <string.h>
  60.  
  61. #define StrSame        0
  62. #define fatal(err)                                                            \
  63.             {fprintf(stderr, "%s(%d):", __FILE__, __LINE__);                \
  64.             fprintf(stderr, " %s\n", err); exit(1);}
  65. #define require(expr, err) {if ( !(expr) ) fatal(err);}
  66.  
  67. static unsigned size = HashTableSize;
  68. static char *strings = NULL;
  69. static char *strp;
  70. static unsigned strsize = StrTableSize;
  71.  
  72. /* create the hash table and string table for terminals (string table only once) */
  73. Entry **
  74. #ifdef __STDC__
  75. newHashTable( void )
  76. #else
  77. newHashTable( )
  78. #endif
  79. {
  80.     Entry **table;
  81.     
  82.     table = (Entry **) calloc(size, sizeof(Entry *));
  83.     require( table != NULL, "cannot allocate hash table");
  84.     if ( strings == NULL )
  85.     {
  86.         strings = (char *) calloc(strsize, sizeof(char));
  87.         require( strings != NULL, "cannot allocate string table");
  88.         strp = strings;
  89.     }
  90.     return table;
  91. }
  92.  
  93. /* Given a table, add 'rec' with key 'key' (add to front of list). return ptr to entry */
  94. Entry *
  95. #ifdef __STDC__
  96. hash_add( Entry **table, char *key, Entry *rec )
  97. #else
  98. hash_add( table, key, rec )
  99. Entry **table;
  100. char *key;
  101. Entry *rec;
  102. #endif
  103. {
  104.     unsigned h=0;
  105.     char *p=key;
  106.     extern Entry *Globals;
  107.     require(table!=NULL && key!=NULL && rec!=NULL, "add: invalid addition");
  108.     
  109.     Hash(p,h,size);
  110.     rec->next = table[h];            /* Add to singly-linked list */
  111.     table[h] = rec;
  112.     return rec;
  113. }
  114.  
  115. /* Return ptr to 1st entry found in table under key (return NULL if none found) */
  116. Entry *
  117. #ifdef __STDC__
  118. hash_get( Entry **table, char *key )
  119. #else
  120. hash_get( table, key )
  121. Entry **table;
  122. char *key;
  123. #endif
  124. {
  125.     unsigned h=0;
  126.     char *p=key;
  127.     Entry *q;
  128.     require(table!=NULL && key!=NULL, "get: invalid table and/or key");
  129.     
  130.     Hash(p,h,size);
  131.     for (q = table[h]; q != NULL; q = q->next)
  132.     {
  133.         if ( strcmp(key, q->str) == StrSame ) return( q );
  134.     }
  135.     return( NULL );
  136. }
  137.  
  138. #ifdef DEBUG_HASH
  139. void
  140. #ifdef __STDC__
  141. hashStat( Entry **table )
  142. #else
  143. hashStat( table )
  144. Entry **table;
  145. #endif
  146. {
  147.     static unsigned short count[20];
  148.     int i,n=0,low=0, hi=0;
  149.     Entry **p;
  150.     float avg=0.0;
  151.     
  152.     for (i=0; i<20; i++) count[i] = 0;
  153.     for (p=table; p<&(table[size]); p++)
  154.     {
  155.         Entry *q = *p;
  156.         int len;
  157.         
  158.         if ( q != NULL && low==0 ) low = p-table;
  159.         len = 0;
  160.         if ( q != NULL ) fprintf(stderr, "[%d]", p-table);
  161.         while ( q != NULL )
  162.         {
  163.             len++;
  164.             n++;
  165.             fprintf(stderr, " %s", q->str);
  166.             q = q->next;
  167.             if ( q == NULL ) fprintf(stderr, "\n");
  168.         }
  169.         count[len]++;
  170.         if ( *p != NULL ) hi = p-table;
  171.     }
  172.  
  173.     fprintf(stderr, "Storing %d recs used %d hash positions out of %d\n",
  174.                     n, size-count[0], size);
  175.     fprintf(stderr, "%f %% utilization\n",
  176.                     ((float)(size-count[0]))/((float)size));
  177.     for (i=0; i<20; i++)
  178.     {
  179.         if ( count[i] != 0 )
  180.         {
  181.             avg += (((float)(i*count[i]))/((float)n)) * i;
  182.             fprintf(stderr, "Bucket len %d == %d (%f %% of recs)\n",
  183.                             i, count[i], ((float)(i*count[i]))/((float)n));
  184.         }
  185.     }
  186.     fprintf(stderr, "Avg bucket length %f\n", avg);
  187.     fprintf(stderr, "Range of hash function: %d..%d\n", low, hi);
  188. }
  189. #endif
  190.  
  191. /* Add a string to the string table and return a pointer to it.
  192.  * Bump the pointer into the string table to next avail position.
  193.  */
  194. char *
  195. #ifdef __STDC__
  196. mystrdup( char *s )
  197. #else
  198. mystrdup( s )
  199. char *s;
  200. #endif
  201. {
  202.     char *start=strp;
  203.     require(s!=NULL, "mystrdup: NULL string");
  204.  
  205.     while ( *s != '\0' )
  206.     {
  207.         require( strp <= &(strings[strsize-2]),
  208.                  "string table overflow\nIncrease StrTableSize in hash.h and recompile hash.c\n");
  209.         *strp++ = *s++;
  210.     }
  211.     *strp++ = '\0';
  212.  
  213.     return( start );
  214. }
  215.